package com.yuntu.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.yuntu.pojo.User;
import com.yuntu.service.UserService;
import com.yuntu.tools.PageUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    @RequestMapping("/doLogin")
    public String doLogin(String devCode, String devPassword, HttpServletRequest request, HttpSession session)throws Exception{
        User user = userService.login(devCode,devPassword);
        if (user != null){
            session.setAttribute("userSession",user);
            return "redirect:/user/getPage";
        }else {
            request.setAttribute("error","用户名或者密码不正确");
            return "login";
        }
    }

    /**
     * 分页显示信息
     * @param pageIndex
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getPage",method = RequestMethod.GET)
    public String getPage(Integer pageIndex, HttpSession session)throws Exception{
        if (pageIndex == null){
            pageIndex = 1;
        }
        PageUtil<User> pageUtil = new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(3);
        userService.getPageList(pageUtil);
        session.setAttribute("pageUtil",pageUtil);
        return "user";
    }

    /**
     * 添加显示页面
     * @return
     */
    @RequestMapping("/add")
    public String addUser(){
        return "add";
    }

    /**
     * 添加验证
     * @param user
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/add.do")
    public String add(User user, HttpSession session)throws Exception{
        User u = (User) session.getAttribute("userSession");
        user.setCreatedBy(u.getId());
        user.setCreationDate(new Date());
        if (userService.add(user)){
            return "redirect:/user/getPage";
        }
        return "add";
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/del/{id}")
    public String delUser(@PathVariable int id)throws Exception{
        if (userService.del(id)){
            return "redirect:/user/getPage";
        }
        return "user";
    }

    /**
     * 根据id查询信息
     * @param id
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/show/{id}")
    public String getId(@PathVariable int id, Model model)throws Exception{
        User user = userService.getUserById(id);
        model.addAttribute(user);
        return "update";
    }

    /**
     * 修改
     * @param user
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/update/{id}")
    public String update(@PathVariable int id, User user, HttpSession session)throws Exception{
        User u = (User) session.getAttribute("userSession");
        user.setModifyBy(u.getId());
        user.setModifyDate(new Date());
        if (userService.update(user)){
            return "redirect:/user/getPage";
        }
        return "update";
    }

    /**
     * 重名验证
     * @param devCode
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/exits.do")
    public Object exits(@RequestParam String devCode)throws Exception{
        Map<String,String> map = new HashMap<>();
        if (StringUtils.isNullOrEmpty(devCode)){
            map.put("devCode","exist");
        }else {
            User user = userService.exist(devCode);
            if (user != null){
                map.put("devCode","exist");
            }else {
                map.put("devCode","noExist");
            }
        }
        return JSONArray.toJSONString(map);
    }
}
